package com.hmbe.controller;

import com.hmbe.dto.Result;
import com.hmbe.dto.StatusCode;
import com.hmbe.entity.HmBeArticle;
import com.hmbe.entity.HmBeCategory;
import com.hmbe.service.HmBeArticleService;
import com.hmbe.service.HmBeCategoryService;
import com.hmbe.utils.MinIOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/my/article")
public class HmBeArticleController {

    @Autowired
    private HmBeArticleService hmBeArticleService;
    @Autowired
    private HmBeCategoryService categoryService;
    @Autowired
    private MinIOUtils minIOUtils;

    @PostMapping("/add")
    public Result save(@RequestParam("categoryId") Long categoryId,
                       @RequestParam("title") String title,
                       @RequestParam("content") String content,
                       @RequestParam("state") String state,
                       @RequestParam("coverImg") MultipartFile coverImg) {
        HmBeArticle hmBeArticle = new HmBeArticle();
        hmBeArticle.setTitle(title);
        hmBeArticle.setContent(content);
        hmBeArticle.setCategoryId(categoryId);
        HmBeCategory category = categoryService.getById(categoryId);
        hmBeArticle.setCategoryName(category.getCateName());
        hmBeArticle.setState(state);
        hmBeArticle.setPublishTime(LocalDateTime.now());
        hmBeArticle.setCreateTime(LocalDateTime.now());
        boolean isSave = false;
        try {
            minIOUtils.uploadFile("default-bucket", title, coverImg);
            String url = minIOUtils.getPresignedObjectUrl("default-bucket", title, 7);
            hmBeArticle.setCoverImgUrl(url);
            isSave = hmBeArticleService.save(hmBeArticle);
        } catch (Exception e) {
            return Result.ok("添加文章失败！", StatusCode.SUCCESS);
        }
        if (!isSave) {
            return Result.ok("添加文章失败！", StatusCode.SUCCESS);
        }
        return Result.ok("添加文章成功！", StatusCode.SUCCESS);

    }

    @PutMapping("/info")
    public Result update(@RequestParam("id") Long id,
                         @RequestParam("categoryId") Long categoryId,
                         @RequestParam("title") String title,
                         @RequestParam("content") String content,
                         @RequestParam("state") String state,
                         @RequestParam("coverImg") MultipartFile coverImg) {
        HmBeArticle hmBeArticle = new HmBeArticle();
        hmBeArticle.setId(id);
        hmBeArticle.setTitle(title);
        hmBeArticle.setContent(content);
        hmBeArticle.setState(state);
        hmBeArticle.setPublishTime(LocalDateTime.now());
        hmBeArticle.setCreateTime(LocalDateTime.now());
        //删除coverImg
        try {
            minIOUtils.deleteFile("default-bucket", title);
            minIOUtils.uploadFile("default-bucket", title, coverImg);
            String url = minIOUtils.getPresignedObjectUrl("default-bucket", title, 7);
            hmBeArticle.setCoverImgUrl(url);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        hmBeArticleService.updateById(hmBeArticle);
        return Result.ok("修改文章成功!", StatusCode.SUCCESS);
    }

    @DeleteMapping("/info")
    public Result delete(@RequestParam("id") Integer id) {
        HmBeArticle article = hmBeArticleService.getById(id);
        try {
            minIOUtils.deleteFile("default-bucket", article.getTitle());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        hmBeArticleService.removeById(id);
        return Result.ok("删除文章成功!", StatusCode.SUCCESS);
    }

    @GetMapping("/info")
    public Result getById(@RequestParam("id") Long id) {
        HmBeArticle article = hmBeArticleService.getById(id);
        return Result.ok("获取文章详情成功!", StatusCode.SUCCESS, article);
    }

    @GetMapping("/list")
    public Result list() {
        List<HmBeArticle> list = new ArrayList<>();
        try {
            list = hmBeArticleService.list();
            hmBeArticleService.list();
        } catch (Exception e) {
            return Result.ok("获取文章列表成功!", StatusCode.SUCCESS, list);
        }
        return Result.ok("获取文章列表成功!", StatusCode.SUCCESS, list, (long) list.size());
    }
}